<template>
    <div class="page createTemporary-page" data-page="createSfifts" style="z-index: 2">
        <div class="link-page-nav">
            <a href="#" class="link" @click=$root.backToTab()> <i class="iconfont icon-zuo"></i>
            </a>
        </div>
        <div class="link-page-top">
            <div class="link-page-title" id='editShiftName_{{getNewId}}'>创建值班记录单</div>
        </div>
        <div class="page-content" style="padding: 0 20px 120px 20px;" id="kalarm">

            <div class="inItem">
                <div><span style="color:red;">*</span>所在单位<label></label></div>
                <a href="#" id="temporaryTask_select_org_{{getNewId}}"><span
                        id="temporaryTask_org_{{getNewId}}">请您选择所在的单位</span><i class="iconfont icon-jiantou"
                        id="temporaryTask_arr_org"></i></a>
            </div>

            <div class="inItem">
                <div><span style="color:red;">*</span>值班人员</div>
                <a href="#" id="selectTemporaryJoner_{{getNewId}}"><span
                        id="temporaryJoiner_{{getNewId}}">选择其他参与人员（多选）</span><i class="iconfont icon-jiantou"></i></a>
            </div>
            <div class="inItem">
                <div><span style="color:red;">*</span>所在消控室</div>
                <input type="text" placeholder="输入所在消控室名称" class="controlRoom" id="controlRoom_{{getNewId}}">
            </div>

            <div class="inItem">
                <div><span style="color:red;">*</span>值班时间段 <span style="color:#E54C2E;font-size:14px;float:right"
                        class="copyTimeTip">已自动为你更新到今天</span>
                </div>
                <a href="#" @click="openTime('start')"><span id="start_time_{{getNewId}}">选择开始时间</span><i
                        class="iconfont icon-jiantou"></i></a>
                <a href="#" @click="openTime('end')"><span id="end_time_{{getNewId}}">选择结束时间</span><i
                        class="iconfont icon-jiantou"></i></a>
            </div>

            <div class="submitBtn" @click="viewBlak_update()">完成</div>
        </div>
    </div>
</template>
<style scoped>
    .controlRoom {
        border-bottom: 1px solid #D2D2D2;
        width: 100%;
        padding-bottom: 5px;
        margin-top: 20px;
    }

    .copyTimeTip {
        display: none;
    }
</style>
<script>
    return {

        mounted() {
            var self = this;
            var app = self.$app;
            if (self.$route.params.createType == 0 && self.$route.params.copy == 'true') {//复制
                jQuery('.copyTimeTip').show()
                let date = new Date();
                let year = date.getFullYear();
                let month = date.getMonth() + 1;
                let day = date.getDate();
                let hour = date.getHours();//获取系统时间
                let minute = date.getMinutes(); //分
                let second = date.getSeconds();//秒
                if (month < 10) {
                    month = "0" + month;
                }
                if (day < 10) {
                    day = "0" + day;
                }
                if (hour < 10) {
                    hour = '0' + hour
                }
                if (minute < 10) {
                    minute = '0' + minute
                }
                if (second < 10) {
                    second = '0' + second
                }
                var nowDate = year + "-" + month + "-" + day + ' ';
                let u = navigator.userAgent;
                var s;
                var e;
                var myDate;
                if (!!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/)) {
                    s = new Date(self.$route.params.shiftsStartTime.replace(/-/g, '/')).getTime()
                    e = new Date(self.$route.params.shiftsEndTime.replace(/-/g, '/')).getTime()

                    myDate = new Date(new Date((moment().locale('zh-cn').format('YYYY-MM-DD') + ' ' + self.$route.params.shiftsStartTime.substring(11)).replace(/-/g, '/')).getTime() + e - s);
                } else {
                    s = new Date(self.$route.params.shiftsStartTime).getTime()
                    e = new Date(self.$route.params.shiftsEndTime).getTime()
                    myDate = new Date(new Date(moment().locale('zh-cn').format('YYYY-MM-DD') + ' ' + self.$route.params.shiftsStartTime.substring(11)).getTime() + e - s);

                }

                $(`#end_time_${self.getNewId}`).html(moment(myDate).format('YYYY-MM-DD HH:mm'));
                $(`#start_time_${self.getNewId}`).html(nowDate + self.$route.params.shiftsStartTime.substring(11, 16));
                self.startTime = nowDate + self.$route.params.shiftsStartTime.substring(11, 16)
                self.endTime = moment(myDate).format('YYYY-MM-DD HH:mm');
                jQuery(`#controlRoom_${self.getNewId}`).val(self.$route.params.controlName)
                self.currentOrgId = self.$route.params.orgId
                if (self.$route.params.dutyAccount.indexOf(userInfor.accountName) > -1) {
                    self.partMan = self.$route.params.dutyAccount
                    self.dutyUserName = self.$route.params.dutyUserName
                } else {
                    self.partMan = self.$route.params.dutyAccount + ',' + userInfor.accountName
                    self.dutyUserName = self.$route.params.dutyUserName + ',' + userInfor.name
                }

                self.selectOptionIndex2 = self.partMan;
                let len = self.partMan.split(',')
                let selectLen = Array.apply(null, new Array(len.length)).map(() => 1)
                self.selectOptionType = selectLen.join(',');
                jQuery(`#temporaryJoiner_${self.getNewId}`).text(self.dutyUserName)
                jQuery(`#temporaryTask_org_${self.getNewId}`).text(self.$route.params.name)

            } else {//新建
                self.startTime = ''
                self.endTime = ''
                jQuery(`#controlRoom_${self.getNewId}`).val('')
                self.currentOrgId = self.$route.params.orgId
                self.partMan = userInfor.accountName
                self.selectOptionIndex2 = self.partMan;
                let len = self.partMan.split(',')
                let selectLen = Array.apply(null, new Array(len.length)).map(() => 1)
                self.selectOptionType = selectLen.join(',');
                jQuery(`#temporaryJoiner_${self.getNewId}`).text(userInfor.name)

            }
            if (self.$route.params.createType != 0 && self.$route.params.copy == 'false') {//编辑
                jQuery(`#editShiftName_${self.getNewId}`).text('值班记录单')
                self.startTime = self.$route.params.shiftsStartTime
                self.endTime = self.$route.params.shiftsEndTime
                $(`#end_time_${self.getNewId}`).html(self.endTime.substring(0, 16));
                $(`#start_time_${self.getNewId}`).html(self.startTime.substring(0, 16));
                jQuery(`#controlRoom_${self.getNewId}`).val(self.$route.params.controlName)
                self.currentOrgId = self.$route.params.orgId
                self.partMan = self.$route.params.dutyAccount
                self.dutyUserName = self.$route.params.dutyUserName
                self.selectOptionIndex2 = self.partMan;
                let len = self.partMan.split(',')
                let selectLen = Array.apply(null, new Array(len.length)).map(() => 1)
                self.selectOptionType = selectLen.join(',');
                jQuery(`#temporaryJoiner_${self.getNewId}`).text(self.dutyUserName)
                jQuery(`#temporaryTask_org_${self.getNewId}`).text(self.$route.params.name)
            }
        },
        data: function () {
            return {
                startTime: '',
                endTime: "",
                currentOrgId: "", //业主单位编码
                dutyMan: "",
                partMan: "",
                tellMan: "",
                verifyMan: userInfor.accountName,
                selectOptionIndex1: 0,
                selectOptionIndex2: "",
                selectOptionIndex3: "",
                selectOptionIndex4: null,
                selectOptionType: null,
                getNewId: (new Date()).getTime(),
                pageName: 'createShifts'
            }
        },
        methods: {
            viewBlak_update: function () { //提交数据
                var self = this;
                var formData = app.form.convertToData('#kalarm');
                var bjms = jQuery.trim(formData.bjms);

                if (self.currentOrgId == "" || self.currentOrgId == undefined || self.currentOrgId == 'undefined' || jQuery(`#temporaryTask_org_${self.getNewId}`).text() == '请您选择所在的单位') {
                    app.methods.showToastCenter("请选择单位");
                    return;
                }
                if (self.partMan == "") {
                    app.methods.showToastCenter("请选择值班人员");
                    return;
                }
                if (jQuery(`#controlRoom_${self.getNewId}`).val() == '') {
                    app.methods.showToastCenter("请输入所在消控室");
                    return;
                }

                if (self.startTime == "") {
                    app.methods.showToastCenter("请选择开始时间");
                    return;
                }
                if (self.endTime == "") {
                    app.methods.showToastCenter("请选择结束时间");
                    return;
                }
                if (new Date(self.startTime).getTime() > new Date(self.endTime).getTime()) {
                    app.methods.showToastCenter("开始时间不能小于结束时间");
                    return;
                }
                common.loading(1);
                Dao.saveShiftsRecord({
                    userName: userInfor.accountName,
                    id: self.$route.params.createType,
                    // deviceId: appKeyObj.deviceId,
                    orgId: self.currentOrgId, //142
                    shiftsEndTimeStr: self.endTime + ':00',
                    shiftsStartTimeStr: self.startTime + ':00',
                    controlName: jQuery(`#controlRoom_${self.getNewId}`).val(),
                    dutyAccount: self.partMan
                }, function (data) {

                    // app.router.back();
                    if (self.$route.params.createType != 0 && self.$route.params.copy == 'false') {
                        common.loading(0);
                        app.methods.showToastCenter("操作成功");
                        app.methods.backToTab();
                    } else {
                        //  app.router.navigate('/changeShifts/',{ignoreCache:true})
                        common.loading(0);
                        app.methods.showToastCenter("操作成功");
                        changeShiftForce = true
                        app.view.current.router.back('/changeShifts/', { force: true })
                    }
                }, true);
            },
            //初始化联网单位
            temporaryTaskInitOrg: function () {
                var self = this;
                if (userInfor.isMuilUnitUser == 1) { //多联网单位角色
                    let organizationId = userInfor.organizationIds.split(',')
                    if(organizationId.length>1){
                        jQuery(`#temporaryTask_select_org_${self.getNewId}`).show().click(function (e) {
                            loadSelectUnitFunc(function (data) {
                                if (self.selectOptionOrg != data.orgId) {
                                    self.selectOptionOrg = data.orgId;
                                    self.currentOrgId = data.orgId;
                                    jQuery(`#temporaryTask_org_${self.getNewId}`).html(data.orgName);
                                    $(`#temporaryTask_org_${self.getNewId}`).css("color", "#333333");
                                } else {
                                    self.selectOptionOrg = 0;
                                    jQuery(`#temporaryTask_org_${self.getNewId}`).html('请您选择所在的单位');
                                    if (single_Orgid == "") {
                                        self.currentOrgId = userInfor.organizationIds;
                                    } else {
                                        self.currentOrgId = single_Orgid;
                                    }
                                }
                            }, false, self.selectOptionOrg);
                        });
                    }else{
                        self.getOrganizationId()
                    }
                    
                } else { //单联网单位角色
                    $("#temporaryTask_arr_org").hide();
                    self.currentOrgId = single_Orgid;
                    jQuery(`#temporaryTask_org_${self.getNewId}`).html(single_OrgName);
                    $(`#temporaryTask_org_${self.getNewId}`).css("color", "#333333");
                }
            },
            getOrganizationId:function(){
                var self = this;
                let unitListDatas = JSON.parse(sessionStorage.getItem('unitListData'))
                $("#temporaryTask_arr_org").hide();
                self.currentOrgId = userInfor.organizationIds;
                jQuery(`#temporaryTask_org_${self.getNewId}`).html(unitListDatas[0].name);
                $(`#temporaryTask_org_${self.getNewId}`).css("color", "#333333");
            },
            initTbPiker: function () {
                var self = this;
                $("#crTb_inputDetails").click(function (e) {
                    //第二个参数是否需要保存语音
                    native.startVoice("desc", false);
                });
            },
            //语音输入回调
            voiceCollectionData: function (voiceStr, componentId) {
                var text = $("#" + componentId).val();
                if (!voiceStr || "" === voiceStr.trim()) {
                    app.methods.showToastCenter("未能识别您说的语音,请重试。");
                } else {
                    $("#" + componentId).val(text + voiceStr);
                }
            },
            openTime: function (type) {
                var self = this;
                let resetTime
                if (type == 'start') {
                    resetTime = jQuery(`#start_time_${self.getNewId}`).text()
                } else {
                    resetTime = jQuery(`#end_time_${self.getNewId}`).text()
                }
                loadSelectedDate_Fn(1, function (data) {
                    let time = new Date(data.singleDate).getTime()
                    if (type == 'start') {
                        if (time > new Date(self.endTime).getTime() && self.endTime != '') {
                            app.methods.showToastCenter("开始时间必须小于时间");
                            return;
                        } else {
                            self.startTime = data.singleDate.substring(0, 17);
                            $(`#start_time_${self.getNewId}`).html(data.singleDate.substring(0, 17));
                        }
                    } else {
                        let time2 = new Date(data.singleDate).getTime()
                        if (time < new Date(self.startTime).getTime() && self.startTime != '') {
                            app.methods.showToastCenter("结束时间必须大于开始时间");
                            return;
                        } else {
                            self.endTime = data.singleDate.substring(0, 17);
                            $(`#end_time_${self.getNewId}`).html(data.singleDate.substring(0, 17));
                        }
                    }
                }, resetTime);
            },
            /*责任人*/
            htmlGetUnitUsers: function () {
                var self = this;
                //多选  值班人员
                jQuery(`#selectTemporaryJoner_${self.getNewId}`).click(function (e) {
                    if (self.currentOrgId == "" || self.currentOrgId == 'undefined' || self.currentOrgId == undefined) {
                        app.methods.showToastCenter('请先请选择单位');
                        return;
                    }
                    //self.$route.params.createType != 0 && self.$route.params.copy == 'false'
                    if (self.$route.params.createType == 0 && self.$route.params.copy == 'true') {
                        let data = {
                            id: self.selectOptionIndex2,
                            index: self.selectOptionIndex2,
                            name: self.dutyUserName,
                            type: self.selectOptionType
                        }
                        getFourTypeUser(self.currentOrgId, function (data) {
                            if (self.$route.params.createAccount.indexOf(userInfor.accountName) < 0) {
                                if (data.id.indexOf(userInfor.accountName) < 0) {
                                    app.methods.showToastCenter('当前用户，不可取消');
                                } else {
                                    if (data.index) {
                                        self.partMan = data.id;
                                        self.selectOptionIndex2 = data.index;
                                        self.selectOptionType = data.type;
                                        $(`#temporaryJoiner_${self.getNewId}`).css("color", "#333").html(data.name);
                                    } else {
                                        self.partMan = "";
                                        self.selectOptionIndex2 = "";
                                        self.selectOptionType = "";
                                        $(`#temporaryJoiner_${self.getNewId}`).css("color", "rgba(153, 153, 153, 1)").html("选择其他参与人员（多选）");
                                    }
                                }
                            }else{
                                if (data.id.indexOf(self.$route.params.createAccount) < 0) {
                                    app.methods.showToastCenter('当前用户，不可取消');
                                } else {
                                    if (data.index) {
                                        self.partMan = data.id;
                                        self.selectOptionIndex2 = data.index;
                                        self.selectOptionType = data.type;
                                        $(`#temporaryJoiner_${self.getNewId}`).css("color", "#333").html(data.name);
                                    } else {
                                        self.partMan = "";
                                        self.selectOptionIndex2 = "";
                                        self.selectOptionType = "";
                                        $(`#temporaryJoiner_${self.getNewId}`).css("color", "rgba(153, 153, 153, 1)").html("选择其他参与人员（多选）");
                                    }
                                }
                            }

                        }, self.selectOptionIndex2, self.selectOptionType, true);

                    }
                    else {
                        // self.selectOptionIndex1 = "";
                        // self.selectOptionIndex2 = "";
                        // self.selectOptionIndex3 = "";
                        // self.selectOptionIndex4 = null;
                        // self.selectOptionType = null;
                        let data = {
                            id: userInfor.accountName,
                            index: userInfor.accountName,
                            name: userInfor.name,
                            type: 1
                        }
                        getFourTypeUser(self.currentOrgId, function (data) {
                            if (data.id.indexOf(userInfor.accountName) < 0) {
                                app.methods.showToastCenter('当前用户，不可取消');
                            } else {
                                if (data.index) {
                                    self.partMan = data.id;
                                    self.selectOptionIndex2 = data.index;
                                    self.selectOptionType = data.type;
                                    $(`#temporaryJoiner_${self.getNewId}`).css("color", "#333").html(data.name);
                                } else {
                                    self.partMan = "";
                                    self.selectOptionIndex2 = "";
                                    self.selectOptionType = "";
                                    $(`#temporaryJoiner_${self.getNewId}`).css("color", "rgba(153, 153, 153, 1)").html("选择其他参与人员（多选）");
                                }
                            }
                        }, self.selectOptionIndex2, self.selectOptionType, true);

                    }
                });

            },
            select_tip: function () {
                var self = this;
                if (self.currentOrgId == "" || self.currentOrgId == 'undefined' || self.currentOrgId == undefined) {
                    app.methods.showToastCenter("请先选择业主单位");
                    return;
                }
            },
        },
        on: {
            pageInit: function (e, page) {
                var self = this;
                $("#temporaryApper").html(userInfor.name + "（单选）");

                self.initTbPiker();
                self.temporaryTaskInitOrg();
                self.htmlGetUnitUsers();
            },
            pageAfterIn: function (e, page) {
                var self = this;
            }
        }
    }
</script>